package crudfx.data;
import java.io.*;
import javafx.io.http.*;
import crudfx.data.*;
import java.net.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class JSONData extends DataTree{
    //var URL:String="http://maps.google.com/maps/nav?output=js&doflg=ptj&q=from%3ABriarcrest%20Rd%2C%2090046%20to%3AHollywood%20Hills%20Rd%2C%2090046%4034.11327%2C-118.39089&callback=_xdc_._0fu5gbb88";
    /*var txt:String="_xdc_._0fu5gbb88 && _xdc_._0fu5gbb88(\{\"name\":\"from:Briarcrest Rd, 90046 to:Hollywood Hills Rd, 90046@34.11327,-118.39089\",\"Status\":\{\"code\":200,\"request\":\"directions\"\},\"Placemark\":\\[\{\"id\":\"\",\"address\":\"Briarcrest Rd, Лос-Анджелес, CA 90046\",\"AddressDetails\":\{\"Country\":\{\"CountryNameCode\":\"US\",\"AdministrativeArea\":\{\"AdministrativeAreaName\":\"CA\",\"SubAdministrativeArea\":\{\"SubAdministrativeAreaName\":\"Los Angeles\",\"Locality\":\{\"LocalityName\":\"Los Angeles\",\"Thoroughfare\":\{\"ThoroughfareName\":\"Briarcrest Rd\"\},\"PostalCode\":\{\"PostalCodeNumber\":\"90046\"\}\}\}\}\},\"Accuracy\": 6\},\"Point\":\{\"coordinates\":\\[-118.392029,34.113332,0\\]\}\},\{\"id\":\"\",\"address\":\"Hollywood Hills Rd, 90046\",\"AddressDetails\":\{\"Thoroughfare\":\{\"ThoroughfareName\":\"Hollywood Hills Rd\"\},\"Accuracy\": 0\},\"Point\":\{\"coordinates\":\\[-118.390890,34.113270,0\\]\}\}\\],\"Directions\":\{\"copyrightsHtml\":\"Данные карты \\u0026#169;2009 Tele Atlas\",\"summaryHtml\":\"5,2\\u0026nbsp;мил. (примерно 13 мин.)\",\"Distance\":\{\"meters\":8340,\"html\":\"5,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":816,\"html\":\"13 мин.\"\},\"Routes\":\\[\{\"Distance\":\{\"meters\":8340,\"html\":\"5,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":816,\"html\":\"13 мин.\"\},\"summaryHtml\":\"5,2\\u0026nbsp;мил. (примерно 13 мин.)\",\"Steps\":\\[\{\"descriptionHtml\":\"Направляйтесь на \\u003Cb\\u003Eсевер\\u003C\\/b\\u003E по \\u003Cb\\u003EBriarcrest Rd\\u003C\\/b\\u003E в направлении \\u003Cb\\u003EAlto Cedro Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":130,\"html\":\"427\\u0026nbsp;фут.\"\},\"Duration\":\{\"seconds\":12,\"html\":\"12 сек\"\},\"Point\":\{\"coordinates\":\\[-118.392030,34.113330,0\\]\},\"polylineIndex\":0\},\{\"descriptionHtml\":\"Поверните \\u003Cb\\u003Eналево\\u003C\\/b\\u003E на пересечении с \\u003Cb\\u003EAlto Cedro Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":87,\"html\":\"285\\u0026nbsp;фут.\"\},\"Duration\":\{\"seconds\":19,\"html\":\"19 сек\"\},\"Point\":\{\"coordinates\":\\[-118.392670,34.114340,0\\]\},\"polylineIndex\":4\},\{\"descriptionHtml\":\"Поверните \\u003Cb\\u003Eналево\\u003C\\/b\\u003E и продолжайте движение по \\u003Cb\\u003EAlto Cedro Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":349,\"html\":\"0,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":45,\"html\":\"45 сек\"\},\"Point\":\{\"coordinates\":\\[-118.393420,34.114760,0\\]\},\"polylineIndex\":8\},\{\"descriptionHtml\":\"Продолжайте движение прямо по \\u003Cb\\u003EEvelyn Pl\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":221,\"html\":\"0,1\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":20,\"html\":\"20 сек\"\},\"Point\":\{\"coordinates\":\\[-118.394900,34.112770,0\\]\},\"polylineIndex\":21\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003ELoma Vista Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":345,\"html\":\"0,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":42,\"html\":\"42 сек\"\},\"Point\":\{\"coordinates\":\\[-118.395850,34.111020,0\\]\},\"polylineIndex\":26\},\{\"descriptionHtml\":\"\\u003Cb\\u003ELoma Vista Dr\\u003C\\/b\\u003E плавно поворачивает \\u003Cb\\u003Eналево\\u003C\\/b\\u003E и переходит в \\u003Cb\\u003ECherokee Ln\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":563,\"html\":\"0,3\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":54,\"html\":\"54 сек\"\},\"Point\":\{\"coordinates\":\\[-118.399190,34.111440,0\\]\},\"polylineIndex\":34\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003EColdwater Canyon Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":2098,\"html\":\"1,3\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":166,\"html\":\"2 мин.\"\},\"Point\":\{\"coordinates\":\\[-118.404870,34.110030,0\\]\},\"polylineIndex\":41\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003EMulholland Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":20,\"html\":\"66\\u0026nbsp;фут.\"\},\"Duration\":\{\"seconds\":11,\"html\":\"11 сек\"\},\"Point\":\{\"coordinates\":\\[-118.403380,34.126270,0\\]\},\"polylineIndex\":72\},\{\"descriptionHtml\":\"Поверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E и продолжайте движение по \\u003Cb\\u003EMulholland Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":2363,\"html\":\"1,5\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":196,\"html\":\"3 мин.\"\},\"Point\":\{\"coordinates\":\\[-118.403160,34.126240,0\\]\},\"polylineIndex\":74\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003ESkyline Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":800,\"html\":\"0,5\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":89,\"html\":\"1 мин.\"\},\"Point\":\{\"coordinates\":\\[-118.386900,34.121880,0\\]\},\"polylineIndex\":121\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003ECrest View Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":248,\"html\":\"0,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":28,\"html\":\"28 сек\"\},\"Point\":\{\"coordinates\":\\[-118.389300,34.118940,0\\]\},\"polylineIndex\":142\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003EGreenvalley Rd\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":141,\"html\":\"463\\u0026nbsp;фут.\"\},\"Duration\":\{\"seconds\":24,\"html\":\"24 сек\"\},\"Point\":\{\"coordinates\":\\[-118.390290,34.117160,0\\]\},\"polylineIndex\":149\},\{\"descriptionHtml\":\"Поверните \\u003Cb\\u003Eналево\\u003C\\/b\\u003E на пересечении с \\u003Cb\\u003EWonderland Park Ave\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":554,\"html\":\"0,3\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":61,\"html\":\"1 мин.\"\},\"Point\":\{\"coordinates\":\\[-118.391740,34.117100,0\\]\},\"polylineIndex\":154\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003ESkyline Dr\\u003C\\/b\\u003E\",\"Distance\":\{\"meters\":35,\"html\":\"115\\u0026nbsp;фут.\"\},\"Duration\":\{\"seconds\":9,\"html\":\"9 сек\"\},\"Point\":\{\"coordinates\":\\[-118.387760,34.113870,0\\]\},\"polylineIndex\":164\},\{\"descriptionHtml\":\"Сверните \\u003Cb\\u003Eнаправо\\u003C\\/b\\u003E на перекрестке с \\u003Cb\\u003EHollywood Hills Rd\\u003C\\/b\\u003E \\u003Cdiv class=\\\"google_note\\\"\\u003EПункт назначения будет справа\\u003C\\/div\\u003E\",\"Distance\":\{\"meters\":386,\"html\":\"0,2\\u0026nbsp;мил.\"\},\"Duration\":\{\"seconds\":40,\"html\":\"40 сек\"\},\"Point\":\{\"coordinates\":\\[-118.387930,34.113600,0\\]\},\"polylineIndex\":167\}\\],\"End\":\{\"coordinates\":\\[-118.390890,34.113220,0\\]\},\"polylineEndIndex\":181\}\\],\"Polyline\":\{\"id\":\"route\",\"points\":\"iweoEdmrqUeAXc@j@_Bx@??AZc@f@m@pA??t@l@h@@`A_@r@G\\\\@z@VLPLv@\\[pB@`@Xb@`AB??v@G`ATxDzBh@r@??W\\\\oAx@OPG^NzDX`C@rE??vA|Bh@tD|@lBVdEp@pCNtJ??yB\}@aJe@\{Cm@yFk@sGGqCSiE~@oBMeAo@aAoBsAm@gEcDk@_Ak@\}Am@oDa@k@u@Qk@@sClB\\]N\{BVsBhAy@nA\\]bBw@dAeBh@sAfAoAd@o@LcAE??Dk@??zA@|Au@nAwBrAsAjAoEtBcCvAiA~AqC^WrA\\]n@i@f@w@^Sh@IrE?tAU~AmAh@qA?_DM\{@\}BiEa@wBcAeB@o@XcAEi@m@\\]oCg@QS_AkCYe@k@o@\}@g@IWBkBAk@IYsBw@\\]_Bq@gBDiBUsB\\]yAHm@|Ag@pEy@??dAhHXj@|@LtCQt@b@Vn@DhAI`@iA`BMb@Cb@Fp@\\\\r@ZRd@HZCx@\\[Z\\]Tk@x@uF??vA\\\\\\\\Xl@hBx@~@\\\\FfBc@??^fCEd@Wn@HbA??vAg@lFmCtAy@fAgAdA_Bh@cBj@_DHwACg@??L?f@`@??Cd@Hj@|AnATvACVQN\\]Cm@WKh@Wd@BX^bAh@bAHvA\",\"levels\":\"B??BB??BB?????@?????BB???BB???@??BB?????BB???????@????????@????????????BBBB?@?????@?????????@???????@??@??????????@???@?BB?@???@??????@????@?BB?????BB???BB???@????BB?BB???@?????@???B\",\"numLevels\":4,\"zoomFactor\":16\},\"jstemplate\":\"\\x3cdiv class\\x3d\\x22googledir\\x22 jsvalues\\x3d\\x22$AllRoutes:Directions.Routes;$numWaypoints:Placemark.length\\x22\\x3e\\x3cdiv jsdisplay\\x3d\\x22walking\\x22 style\\x3d\\x22margin-top:5px;margin-right:0;margin-bottom:3px;margin-left:0\\x22\\x3e\\x3cdiv style\\x3d\\x22height:1px;font-size:.01em;overflow:hidden;margin-top:0;margin-right:2px;background:#fff1a8\\x22\\x3e\\x3c/div\\x3e\\x3cdiv style\\x3d\\x22height:1px;font-size:.01em;overflow:hidden;margin-top:0;margin-right:1px;background:#fff1a8\\x22\\x3e\\x3c/div\\x3e\\x3cdiv style\\x3d\\x22padding:5px 6px;background:#fff1a8\\x22\\x3e\\x3cb style\\x3d\\x22padding-left:20px\\x22\\x3eПешие маршруты находятся в режиме бета-тестирования.\\x3c/b\\x3e\\x3cbr/\\x3eБудьте внимательны \\x26ndash; В маршруте могут быть не указаны тротуары или пешеходные дорожки.\\x3c/div\\x3e\\x3cdiv style\\x3d\\x22height:1px;font-size:.01em;overflow:hidden;margin-top:0;margin-right:1px;background:#fff1a8\\x22\\x3e\\x3c/div\\x3e\\x3cdiv style\\x3d\\x22height:1px;font-size:.01em;overflow:hidden;margin-top:0;margin-right:2px;background:#fff1a8\\x22\\x3e\\x3c/div\\x3e\\x3c/div\\x3e\\x3cdiv jsselect\\x3d\\x22Placemark\\x22\\x3e\\x3ctable style\\x3d\\x22margin:10px 0px 10px 0px;border:1px solid silver;background:#eee;border-collapse:collapse;color:#000\\x22\\x3e\\x3ctr style\\x3d\\x22cursor:pointer\\x22 jsvalues\\x3d\\x22.waypoint:$index\\x22 xonclick\\x3d\\x22dirapiShowMapBlowup(this.waypoint, -1)\\x22\\x3e\\x3ctd style\\x3d\\x22vertical-align:middle;width:20px;padding-top:4px;padding-right:15px;padding-bottom:0;padding-left:5px\\x22\\x3e\\x3cimg jsvalues\\x3d\\x22src:markerIconPaths\\[$index\\]\\x22\\x3e\\x3c/td\\x3e\\x3ctd style\\x3d\\x22vertical-align:middle;width:100%\\x22jscontent\\x3d\\x22address\\x22/\\x3e\\x3c/tr\\x3e\\x3c/table\\x3e\\x3cdiv jsdisplay\\x3d\\x22$AllRoutes\\[$index\\] !\\x3d null\\x22 jsvalues\\x3d\\x22$routeId:$index;$Route:$AllRoutes\\[$index\\]\\x22\\x3e\\x3cdiv style\\x3d\\x22text-align:right;padding-bottom:.3em\\x22 jseval\\x3d\\x22this.innerHTML \\x3d $Route.summaryHtml\\x22\\x3e\\x3c/div\\x3e\\x3ctable style\\x3d\\x22border-collapse:collapse;margin:0;padding:0\\x22jsdisplay\\x3d\\x22$Route.Steps.length !\\x3d 1 ||$Route.Steps\\[0\\].descriptionHtml !\\x3d \\x27\\x27\\x22\\x3e\\x3ctr style\\x3d\\x22cursor:pointer\\x22 jsselect\\x3d\\x22$Route.Steps\\x22 jsvalues\\x3d\\x22.route:$routeId;.step:$index\\x22 xonclick\\x3d\\x22dirapiShowMapBlowup(this.route, this.step)\\x22\\x3e\\x3ctd style\\x3d\\x22vertical-align:top;border-top:1px solid #cdcdcd;padding:.3em 3px .3em 3px;margin:0;text-align:right\\x22\\x3e\\x3ca href\\x3d\\x22javascript:void(0)\\x22 jscontent\\x3d\\x22($index + 1)\\x22\\x3e\\x3c/a\\x3e.\\x3c/td\\x3e\\x3ctd style\\x3d\\x22vertical-align:top;border-top:1px solid #cdcdcd;padding:.3em 3px .3em 3px;margin:0;width:100%\\x22jseval\\x3d\\x22this.innerHTML \\x3d descriptionHtml\\x22/\\x3e\\x3ctd style\\x3d\\x22vertical-align:top;border-top:1px solid #cdcdcd;padding-top:.3em;padding-right:3px;padding-bottom:.3em;padding-left:.5em;margin:0;text-align:right\\x22jseval\\x3d\\x22this.innerHTML \\x3d Distance.html\\x22/\\x3e\\x3c/tr\\x3e\\x3c/table\\x3e\\x3c/div\\x3e\\x3c/div\\x3e\\x3cdiv style\\x3d\\x22font-size:.86em\\x22 jseval\\x3d\\x22this.innerHTML \\x3d Directions.copyrightsHtml\\x22/\\x3e\\x3c/div\\x3e\"\}\})";
    var txt2:String="\{"
    "       \"firstName\": \"Иван\","
    "       \"lastName\": \"Иванов\","
    "       \"address\": \{"
    "           \"streetAddress\": \"Московское ш., 101, кв.101\","
    "           \"city\": \"Ленинград\","
    "           \"postalCode\": 101101"
    "           \},"
    "       \"phoneNumbers\": ["
    "           \"812 123-1234\","
    "           \"916 123-4567\""
    "           ]"
    "       \}";*/
    var piece:String="";
    var currentPropertyName:String="";
    var doneParsing:Boolean;
    var reader:Reader;
    function readNext():Boolean{
        piece="";
        var hasData:Boolean=false;
        var quotes:Boolean=false;
        var presymbol:String="";
        var symbol:String;
        var cont:Boolean=true;
        var i:Number=reader.read();
        while(i>-1 and cont){
            symbol=crudfx.util.Tools.i2s(i);
            hasData=true;
            if(presymbol.equals('\\'))symbol="\\{symbol}";
            if(symbol.equals('"'))quotes=not quotes;
            piece="{piece}{symbol}";
            if(not quotes){
                if(symbol.equals('\{')
                    or symbol.equals('\}')
                    or symbol.equals("[")
                    or symbol.equals("]")
                    or symbol.equals(':')
                    or symbol.equals(','))cont=false;
                }
            presymbol=symbol;
            if(cont)i=reader.read();
            }
        piece=piece.trim();
        //println("\tpiece={piece}");
        return hasData;
        }
    function addProperty():Void{
        piece=piece.substring(0,piece.length()-1);
        piece=piece.trim();
        if(piece.substring(piece.length()-1).equals('"'))piece=piece.substring(0,piece.length()-1);
        if(piece.substring(0,1).equals('"'))piece=piece.substring(1,piece.length());
        //println("property: {piece}");
        currentPropertyName=piece;
        }
    function setProperty():Void{
        if(piece.length()<2)return;
        if(piece.substring(piece.length()-1).equals('\}'))doneParsing=true;
        piece=piece.substring(0,piece.length()-1);
        piece=piece.trim();
        if(piece.substring(piece.length()-1).equals(',')){
            piece=piece.substring(0,piece.length()-1);
            piece=piece.trim();
            }
        if(piece.substring(piece.length()-1).equals('"'))piece=piece.substring(0,piece.length()-1);
        if(piece.substring(0,1).equals('"'))piece=piece.substring(1,piece.length());
        //println("value: {piece}");
        //node(currentPropertyName).asString("").value=piece;
        var p:DataTree=DataTree{
            name:currentPropertyName
            };
        p.asString("").value=piece;
        insert p into children;
        }
    function readObjectProperty():Void{
        //println("obj<<");
        var sub:JSONData=JSONData{
            name:currentPropertyName
            };
        sub.reader=reader;
        sub.parse();
        //println(">>obj");
        insert sub into children;
        }
    function readArrayProperty():Void{
        //println("value: {pname}");
        var endArray:Boolean=false;
        //readNext();
        while(readNext() and not endArray){
            if(piece.substring(piece.length()-1).equals(']'))endArray=true;
            if(piece.substring(piece.length()-1).equals('\{'))readObjectProperty()
            else setProperty();
            //println(piece);
            }
        }
    function skip():Void{
        while(readNext() and (not piece.substring(piece.length()-1).equals('\{'))){
            //skip
            }
        }
    public function query(URL:String):Void{
        var url:URL=new URL(URL);
        try{
            reader=new InputStreamReader(url.openStream());
            skip();
            parse();
            reader.close();
            }catch(x){
                x.printStackTrace();
                }
        }
    public function parse():Void{
        doneParsing=false;
        while(readNext() and not doneParsing){
            if(piece.substring(piece.length()-1).equals(':')){
                addProperty();
                readNext();
                if(piece.substring(piece.length()-1).equals('\{'))readObjectProperty()
                else if(piece.substring(piece.length()-1).equals('['))readArrayProperty()
                else setProperty();
                }
            }
        }
    }